package com.yeqing.niodemo;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Write {

    public static void write() throws Exception {
        try (
                FileOutputStream fileOutputStream = new FileOutputStream("writesomebytes.txt");
                FileChannel channel = fileOutputStream.getChannel();
        ) {
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            String message = "hello world";
            buffer.put(message.getBytes());
            buffer.slice();
            buffer.flip();
            channel.write(buffer);
        }
    }

    public static void main(String[] args) {
        try {
            write();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
